package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.SysDept;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysCredentialsType;
import com.ruoyi.project.system.service.ISysCredentialsTypeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 证书类型Controller
 *
 * @author ruoyi
 * @date 2020-04-26
 */
@RestController
@RequestMapping("/system/credentialsType")
public class SysCredentialsTypeController extends BaseController {
    @Autowired
    private ISysCredentialsTypeService sysCredentialsTypeService;

    /**
     * 查询证书类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:credentialsType:list')")
    @GetMapping("/list")
    public AjaxResult list(SysCredentialsType sysCredentialsType) {
        List<SysCredentialsType> list = sysCredentialsTypeService.selectSysCredentialsTypeList(sysCredentialsType);
        return AjaxResult.success(list);
    }

    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysCredentialsType sysCredentialsType) {
        List<SysCredentialsType> sysCredentialsTypes = sysCredentialsTypeService.selectSysCredentialsTypeList(sysCredentialsType);
        return AjaxResult.success(sysCredentialsTypeService.buildDeptTreeSelect(sysCredentialsTypes));
    }


    /**
     * 导出证书类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:credentialsType:export')")
    @Log(title = "证书类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysCredentialsType sysCredentialsType) {
        List<SysCredentialsType> list = sysCredentialsTypeService.selectSysCredentialsTypeList(sysCredentialsType);
        ExcelUtil<SysCredentialsType> util = new ExcelUtil<SysCredentialsType>(SysCredentialsType.class);
        return util.exportExcel(list, "credentialsType");
    }

    /**
     * 获取证书类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:credentialsType:query')")
    @GetMapping(value = "/{credentialsTypeId}")
    public AjaxResult getInfo(@PathVariable("credentialsTypeId") Long credentialsTypeId) {
        return AjaxResult.success(sysCredentialsTypeService.selectSysCredentialsTypeById(credentialsTypeId));
    }

    /**
     * 新增证书类型
     */
    @PreAuthorize("@ss.hasPermi('system:credentialsType:add')")
    @Log(title = "证书类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCredentialsType sysCredentialsType) {
        // 判断证书名称是否已经存在
        if (UserConstants.NOT_UNIQUE.equals(sysCredentialsTypeService.checkTypeNameUnique(sysCredentialsType))) {
            return AjaxResult.error("新增类型'" + sysCredentialsType.getCredentialsType() + "'失败，类型名称已存在");
        }
        sysCredentialsType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysCredentialsTypeService.insertSysCredentialsType(sysCredentialsType));
    }

    /**
     * 修改证书类型
     */
    @PreAuthorize("@ss.hasPermi('system:credentialsType:edit')")
    @Log(title = "证书类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCredentialsType sysCredentialsType) {
        return toAjax(sysCredentialsTypeService.updateSysCredentialsType(sysCredentialsType));
    }

    /**
     * 删除证书类型
     */
    @PreAuthorize("@ss.hasPermi('system:credentialsType:remove')")
    @Log(title = "证书类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{credentialsTypeIds}")
    public AjaxResult remove(@PathVariable Long[] credentialsTypeIds) {
        return toAjax(sysCredentialsTypeService.deleteSysCredentialsTypeByIds(credentialsTypeIds));
    }
    
}
